/*==============================================================================
             Copyright (c) 2009 - All rights reserved
================================================================================
File description:

    Timeline function definition

================================================================================
      Date            Name                    Description of Change
03-10-2009  Xiaofeng.Gu                    Written
$HISTORY$
==============================================================================*/

#include "stdafx.h"
#include "Timeline.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CTimeline::CTimeline()
{
	m_strTimelineName = _T("Timeline");
}

CTimeline::~CTimeline()
{
	for (Families::iterator it = m_oFamilies.begin();
		it != m_oFamilies.end(); it ++)
	{
		delete *it;
		*it = NULL;
	}
	m_oFamilies.clear();
}

bool CTimeline::OnRead(CXMLParser* pParser)
{
	if (pParser->FindXMLNode(NODE_TIMELINE))
	{
		m_strTimelineName = pParser->GetName();
			while (pParser->FindChildNode(NODE_FAMILY))
			{
				CFamily* pFamily = new CFamily();
				m_oFamilies.push_back(pFamily);
					pFamily->OnRead(pParser);
				pParser->EndXMLNode();
			}
		pParser->EndXMLNode();
	}

	return __super::OnRead(pParser);
}

bool CTimeline::OnWrite(CXMLParser* pParser)
{
	pParser->AddXMLNode(NODE_TIMELINE);
	pParser->AddName(GetName());
		for (Families::iterator it = m_oFamilies.begin();
			it != m_oFamilies.end(); it ++)
		{
			CFamily* pFamily = *it;
			pFamily->OnWrite(pParser);
		}
	pParser->EndXMLNode();

	return __super::OnWrite(pParser);
}

void CTimeline::GetFramesByIndex(long lIndex, Frames* pFrames)
{
	pFrames->clear();
	for (Families::iterator it = m_oFamilies.begin(); it != m_oFamilies.end(); it ++)
	{
		CFamily* pFamily = *it;
		CFrame* pFrame = pFamily->GetFrameByIndex(lIndex);
		if (pFrame != NULL)
		{
			pFrames->push_back(pFrame);
		}
	}
}

CFamily* CTimeline::GetActiveFamily()
{
	for (Families::iterator itFa = m_oFamilies.begin(); itFa != m_oFamilies.end(); itFa ++)
	{
		CFamily* pFamily = *itFa;
		if (pFamily->GetActive()) return pFamily;
	}

	return NULL;
}

void CTimeline::Reset()
{
	for (Families::iterator itFa = m_oFamilies.begin(); itFa != m_oFamilies.end(); itFa ++)
	{
		CFamily* pFamily = *itFa;
		pFamily->Clear();
	}

}